package com.linln.admin.pcs.controller;


import com.linln.admin.pcs.service.VerifyLogService;
import com.linln.admin.system.service.AppService;
import com.linln.common.data.QuerySpec;
import com.linln.common.utils.HttpServletUtil;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.utils.ValidationUtil;
import com.linln.component.actionLog.annotation.EntityParam;
import com.linln.component.shiro.ShiroUtil;
import com.linln.modules.system.domain.CompanyParam;
import com.linln.modules.system.domain.Complain;
import com.linln.modules.system.domain.ContractTpl;
import com.linln.modules.system.domain.User;
import com.linln.modules.system.repository.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 参数配置管理
 */
@Controller
@RequestMapping("/pcs/companyParam")
public class CompanyParamController {

    @Autowired
    private AppService appService;
    @Autowired
    private  ProjectRepository projectRepository;
    @Autowired
    private CompanyRepository companyRepository;
    @Autowired
    private PorterRepository porterRepository;
    @Autowired
    private ContractTplRepository contractTplRepository;

    @Autowired
    private VerifyLogService verifyLogService;
    @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;
    @Autowired
    private CompanyParamRepository companyParamRepository;

    /**
     * 公司参数页面
     */
    @GetMapping("/index")
    @RequiresPermissions("pcs:companyParam:index")
    public String index(Model model) {
        User user = ShiroUtil.getSubject();
        QuerySpec querySpec = QuerySpec.matching();
        Long companyId = user.getCompanyId();
        querySpec.withMatcherValue("companyId", QuerySpec.EQUAL,companyId.toString());
        List<CompanyParam> companyParamList = companyParamRepository.findAll(QuerySpec.of(querySpec));
        if (companyParamList.size()>0){
            model.addAttribute("vo", companyParamList.get(0));
        }else{
            model.addAttribute("vo", null);
        }
        return  "/pcs/company_param/form";
    }

    /**
     * 项目审核
     * @return
     * @throws Exception
     */
    @RequestMapping("/form")
    @RequiresPermissions("pcs:companyParam:save")
    public ResponseEntity<?> save(@EntityParam CompanyParam companyParam) throws Exception {
        User user = ShiroUtil.getSubject();
        Long companyId = user.getCompanyId();
        companyParam.setCompanyId(companyId);
        QuerySpec querySpec = QuerySpec.matching();
        querySpec.withMatcherValue("companyId", QuerySpec.EQUAL,companyId.toString());
        List<CompanyParam> companyParamList = companyParamRepository.findAll(QuerySpec.of(querySpec));
        if (companyParamList.size()>0){
            companyParam.setId(companyParamList.get(0).getId());
        }
        companyParamRepository.save(companyParam);
        return ResponseEntity.ok(ResultVoUtil.success("成功"));

    }








}
